package com.liang.maogouyuyin.utils

import android.icu.util.Calendar
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale

object DateUtil {
    private val calendar = Calendar.getInstance()

    fun formatTimeStamp(timestamp: Long, pattern: String = "yyyy-MM-dd"): String {
        val date = Date(timestamp)
        val sdf = SimpleDateFormat(pattern, Locale.getDefault())
        return sdf.format(date)
    }

    fun getAge(birthday: Long): Int {
        var timestamp = System.currentTimeMillis() - birthday
        timestamp /= 1000L
        return (timestamp / (365L * 24 * 60 * 60)).toInt()
    }

    fun getDays(arriveTime: Long): Int {
        var timestamp = System.currentTimeMillis() - arriveTime
        timestamp /= 1000L
        return (timestamp / (24L * 60 * 60)).toInt()
    }

    fun getStartTimestampOfToday(): Long {
        calendar.apply {
            set(Calendar.HOUR_OF_DAY, 0)
            set(Calendar.MINUTE, 0)
            set(Calendar.SECOND, 0)
            set(Calendar.MILLISECOND, 0)
        }
        return calendar.timeInMillis
    }

    fun getStartTimestampForDate(date: Date): Long {
        calendar.apply {
            time = date
            set(Calendar.HOUR_OF_DAY, 0)
            set(Calendar.MINUTE, 0)
            set(Calendar.SECOND, 0)
            set(Calendar.MILLISECOND, 0)
        }
        return calendar.timeInMillis
    }

    fun getStartTimestampForMonth(date: Date): Long {
        calendar.apply {
            time = date
            set(Calendar.DAY_OF_MONTH, 1)
            set(Calendar.HOUR_OF_DAY, 0)
            set(Calendar.MINUTE, 0)
            set(Calendar.SECOND, 0)
            set(Calendar.MILLISECOND, 0)
        }
        return calendar.timeInMillis
    }

    fun getDaysForMonth(date: Date): Int {
        calendar.apply {
            time = date
        }
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH)
    }
}